package com.study.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.model.mdao.OrderFormMapper;
import com.study.model.pojo.Addresdetail;
import com.study.services.OrderService;
import com.study.util.MaxId;

/**
 * 订单详情控制层
 * 
 * @author 郭星
 *
 */
@Controller
public class OrderDetailController {

	@Autowired
	OrderService service;
	@Autowired
	OrderFormMapper orderform;

	/**
	 * 访问地址http://localhost:8089/goods
	 * 
	 * @return
	 *
	 */
	@RequestMapping("goods")
	public String first(HttpSession session) {
		// List<Orderform> list = service.selectByPager(1, 8);
		// session.setAttribute("DICTIONARY43", list);
		return "onSale/goods";
	}

	/**
	 * 分页查询
	 */
	@PostMapping("/onSale/goodse")
	@ResponseBody
	public String getForm1(HttpSession session) {
		int uid = (int) session.getAttribute("useid");
		List<JSONObject> list = new ArrayList<>();
		list = service.selectByPagerSqlOrderDetail("", "", "", 1, 10, uid);
		String json ="";
		if (list != null && list.size() > 0) {
		formatDate(list);

		
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);

		json = JSONObject.toJSONString(pager);
		}

		System.err.println("这是订单明细信息：" + json);
		return json;
	}

	/**
	 * oreder分页、全部、带条件查询
	 * 
	 * @param selects     搜索框条件头
	 * @param selectinput 搜索框条件中
	 * @param selectend   搜索框条件尾
	 * @param currentPage 当前页
	 * @param pagesize    页大小
	 * @return
	 */
	@PostMapping("/onSale/goodse2")
	@ResponseBody
	public String getForm4(String selects, String selectinput, String selectend, int currentPage, int pagesize,
			HttpSession session) {
		System.out.println(currentPage + "currentPage");
		System.out.println(pagesize + "pagesize");
		System.out.println(selectinput);
		System.out.println(selects);
		if (selects.equals("全部数据")) {
			selects = null;
		}

		int uid = (int) session.getAttribute("useid");
		List<JSONObject> list = service.selectByPagerSqlOrderDetail(selects, selectinput, selectend, currentPage,
				pagesize, uid);
		String json ="";
		if (list != null && list.size() > 0) {
		formatDate(list);

		
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);

		json = JSONObject.toJSONString(pager);
		}
		System.err.println("这是订单明细信息：" + json);
		return json;
	}

	/**
	 * 处理list集合中的时间格式
	 * 
	 * @param list
	 */
	public static void formatDate(List<JSONObject> list) {

		int i = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		if (list != null && list.size() > 0) {
			for (JSONObject jsonObject : list) {
				System.out.println(jsonObject);
				String string = jsonObject.getString("o_date");
				if (string != null && string.length() > 0) {
					try {

						Date parse = format.parse(string);
						System.out.println(format.format(parse));
						list.get(i).put("o_date", format.format(parse).toString());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				i++;

			}
		}
	}

	/**
	 * 添加发货单
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("/onSale/addFaHuo")
	@ResponseBody
	public Integer addFaHuo(Integer id) {
		System.out.println("当前订单id为：" + id);
		service.addFahuo(id);
		return id;
	}

	/**
	 * 分页查询
	 */
	@PostMapping("/onSale/fahuo")
	@ResponseBody
	public String getFahuo(HttpSession session) {
		int uid = (int) session.getAttribute("useid");
		List<JSONObject> list = service.selectByPagerSqlFaHuo("", "", "", 1, 10, uid);
		// formatDate(list);
		String json = "";
		if (list!=null&&list.size()>0) {
			int i = 0;
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			for (JSONObject jsonObject : list) {
				System.out.println(jsonObject);
				String string = jsonObject.getString("ded_date");
				if (string != null && string.length() > 0) {
					try {

						Date parse = format.parse(string);
						System.out.println(format.format(parse));
						list.get(i).put("ded_date", format.format(parse).toString());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				i++;

			}

			PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
			json=JSONObject.toJSONString(pager);
		}
		

	

		System.err.println("这是发货信息：" + json);
		return json;
	}

	/**
	 * oreder分页、全部、带条件查询
	 * 
	 * @param selects     搜索框条件头
	 * @param selectinput 搜索框条件中
	 * @param selectend   搜索框条件尾
	 * @param currentPage 当前页
	 * @param pagesize    页大小
	 * @return
	 */
	@PostMapping("/onSale/fahuo2")
	@ResponseBody
	public String getfahuo2(String selects, String selectinput, String selectend, int currentPage, int pagesize,
			HttpSession session) {
		System.out.println(currentPage + "currentPage");
		System.out.println(pagesize + "pagesize");
		System.out.println(selectinput);
		System.out.println(selects);
		if (selects.equals("全部数据")) {
			selects = null;
		}

		int uid = (int) session.getAttribute("useid");
		List<JSONObject> list = service.selectByPagerSqlFaHuo(selects, selectinput, selectend, currentPage, pagesize,
				uid);
		String json = "";
		if (list!=null&&list.size()>0) {
			int i = 0;
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			for (JSONObject jsonObject : list) {
				System.out.println(jsonObject);
				String string = jsonObject.getString("ded_date");
				if (string != null && string.length() > 0) {
					try {

						Date parse = format.parse(string);
						System.out.println(format.format(parse));
						list.get(i).put("ded_date", format.format(parse).toString());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				i++;

			}

			PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
			json=JSONObject.toJSONString(pager);
		}
		System.err.println("这是发货信息：" + json);
		return json;
	}

	/**
	 * 传入ifram表单信息
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/fahuoidgoods")
	public ModelAndView first2(Integer id, HttpSession session) {
		System.out.println("fahuoid:iframe" + id);
		ModelAndView mv = new ModelAndView("/onSale/fahuolog");
		JSONObject findBydedid = service.findBydedid(id);
		mv.addObject("textfahuo", findBydedid);
		System.out.println(findBydedid);
		return mv;
	}

	/**
	 * 发货：发货详情商品
	 * 
	 * @param id         发货详情id
	 * @param faid       发货表id
	 * @param havetime   发货时间
	 * @param havegmoney 发货金额
	 * @param oid        订单Id
	 * @param odid       订单详情表id
	 */
	@PostMapping("/onSale/doFaHuoOk")
	@ResponseBody
	public Integer doFaHuo(Integer id, Integer faid, Date havetime, double havegmoney, Integer oid, Integer odid) {
		System.err.println("当前发货详情id为：" + id + "当前发货单id为：" + faid + " 设置发货日期为：" + havetime + " 邮费为：" + havegmoney
				+ " 订单id为：" + oid + " 订单详情id为：" + odid);
		service.doFaHuo(id, faid, havetime, havegmoney, oid, odid);

		return id;
	}

	/**
	 * 设置用户联系地址
	 */

	@PostMapping("addareaAll")
	@ResponseBody
	public String getArea(String adress, String id, String oid, HttpServletRequest request) {
		System.out.println(adress + id + "--------" + oid);
		int setAd = service.setAd(adress, id, oid);
		// 修改订单的收货地址为其
		orderform.updateAddr(setAd, Integer.parseInt(oid));
		return "c";
	}

}
